<?php

if (!defined('UNDER_WEBSERVER')) {
    if (substr(PHP_SAPI, 0, 3) != 'cli') {
        exit('Only Can Be Run Under Cli!');
    }
}

ini_set('memory_limit', -1);
error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set('Asia/Chongqing');
define('FRAMEWORK_PATH', dirname(__FILE__));
define('FRAMEWORK_STARTPHP', 'KingPHP.php');

//常量说明
//APP_PATH
//APP_LOG_PATH
//APP_DATA_PATH
//初始化
function framework_init() {
    $trace = debug_backtrace();
    foreach ($trace as $k => $v) {
        if (strtolower($v['function']) == 'require' || 'require_once' == strtolower($v['function'])) {
            if (strpos($v['args'][0], FRAMEWORK_STARTPHP) && substr($v['args'][0], -strlen(FRAMEWORK_STARTPHP)) == FRAMEWORK_STARTPHP) {
                define('APP_PATH', dirname($v['file']));
                define('APP_LOG_PATH', APP_PATH . '/Runtime/logs/');
                define('APP_DATA_PATH', APP_PATH . '/Runtime/data/');
                define('OUTPUT_LOGDIR', APP_LOG_PATH);
                if (defined('FORUM_DOMAIN')) {
                    define('APP_USER_EMAIL_HAS_REGISTER_FILE', APP_DATA_PATH . FORUM_DOMAIN . '/' . 'User-Email-Has-Registered.txt');
                    define('APP_USER_PASS_UID_REGISTER_FILE', APP_DATA_PATH . FORUM_DOMAIN . '/' . 'User-Pass-Uid-Registered.txt');
                }
                if (!defined('UNDER_WEBSERVER')&&!defined('UN_ECHO_INIT')) {
                    echo date('Y-m-d H:i:s') . '|NOTICE:Framework Init() Success|' . PHP_EOL;
                    echo date('Y-m-d H:i:s') . '|NOTICE:Framework Init() :|' . 'APP_PATH:' . APP_PATH . '|' . PHP_EOL;
                    echo date('Y-m-d H:i:s') . '|NOTICE:Framework Init() :|' . 'APP_LOG_PATH:' . APP_LOG_PATH . '|' . PHP_EOL;
                    echo date('Y-m-d H:i:s') . '|NOTICE:Framework Init() :|' . 'APP_DATA_PATH:' . APP_DATA_PATH . '|' . PHP_EOL;
                    echo date('Y-m-d H:i:s') . '|NOTICE:Framework Init() :|' . 'OUTPUT_LOGDIR:' . OUTPUT_LOGDIR . '|' . PHP_EOL;
                }
                return true;
            }
        }
    }
    echo date('Y-m-d H:i:s') . '|ERROR:Framework Init() Failed|' . $traceInfo . '|' . PHP_EOL;
    exit();
    return false;
}

framework_init();

if (!defined('DATA_USER_RAND_LIST_TXT_FILE')) {
    if (mt_rand(1, 5) > 3) {
        define('DATA_USER_RAND_LIST_TXT_FILE', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'UserList' . DIRECTORY_SEPARATOR . 'NameLib.txt');
    } else {
        define('DATA_USER_RAND_LIST_TXT_FILE', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'UserList' . DIRECTORY_SEPARATOR . 'NameLib_' . mt_rand(1, 2) . '.txt');
    }
}

if (!defined('DATA_EMAIL_RAND_LIST_TXT_FILE')) {
    define('DATA_EMAIL_RAND_LIST_TXT_FILE', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'EmailList' . DIRECTORY_SEPARATOR . 'EmailLib.txt');
}

//SpiderReplaceContent.txt
if (!defined('SPIDER_REPLACE_LIST_TXT_FILE')) {
    define('SPIDER_REPLACE_LIST_TXT_FILE', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'Other' . DIRECTORY_SEPARATOR . 'SpiderReplaceContent.txt');
}

$http_status_code = 200;
$dhttp_response_header = array();
$dhttp_lastredirectaddr = false;

#日志输出目录
if (!defined('OUTPUT_LOGDIR')) {
    define('OUTPUT_LOGDIR', FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR);
}

#是否记录文本日志
if (!defined('DEFAULT_LOGLOG')) {
    define('DEFAULT_LOGLOG', false);
}

#是否输出标准日志
if (!defined('DEFAULT_ECHOLOG')) {
    define('DEFAULT_ECHOLOG', true);
}

$arrGlobalRuntimeEnvVars = array();
if (strpos('a' . strtoupper(PHP_OS), 'WIN')) {
    $arrGlobalRuntimeEnvVars['cmd']['cat'] = 'type';
    $arrGlobalRuntimeEnvVars['cmd']['wc -l'] = 'find /v /c ""';
}
if (strpos('a' . strtoupper(PHP_OS), 'LINUX')) {
    $arrGlobalRuntimeEnvVars['cmd']['cat'] = 'cat';
    $arrGlobalRuntimeEnvVars['cmd']['wc -l'] = 'wc -l';
}

    $arrGlobalRuntimeEnvVars['cmd']['cat'] = 'cat';
    $arrGlobalRuntimeEnvVars['cmd']['wc -l'] = 'wc -l';

$GLOBALS['varStore'] = array();
$GLOBALS['varStore']['fileCount'] = array();
$GLOBALS['varStore']['hasRegisterUser'] = array();
$GLOBALS['varStore']['spiderReplaceContent'] = array();
if (defined('APP_USER_EMAIL_HAS_REGISTER_FILE') && file_exists(APP_USER_EMAIL_HAS_REGISTER_FILE)) {
    $tempVar = array();
    $tempVar = file(APP_USER_EMAIL_HAS_REGISTER_FILE);
    foreach ($tempVar as &$v) {
        $v = trim($v);
    }unset($v);
    $GLOBALS['varStore']['hasRegisterUser'] = $tempVar;
    unset($tempVar);

    if (file_exists(APP_USER_PASS_UID_REGISTER_FILE)) {
        $tempVar = array();
        $tempVar = file(APP_USER_PASS_UID_REGISTER_FILE);
        foreach ($tempVar as &$v) {
            $v = trim($v);
            $v = json_decode($v, true);
            ;
        }unset($v);
        $GLOBALS['varStore']['UserPassUid'] = $tempVar;
        unset($tempVar);
    }
}

//屏蔽PHP-Spider抓取的域名
$GLOBALS['varStore']['pingBiDomainList'] = array(
    'google.com',
    '58.com',
    'www.baidu.com',
    'ganji.com',
    'liebiao.com'
);

/*
  ini_set('memory_limit', -1);
  error_reporting(E_ERROR);
  define('FROM_DB_MYSQL_HOST', '127.0.0.1');
  define('FROM_DB_MYSQL_USER', 'root');
  define('FROM_DB_MYSQL_PASS', '123');
  define('FROM_DB_MYSQL_DBNAME', 'update');
  define('FROM_DB_MYSQL_PORT', 3306);
  define('FROM_DB_MYSQL_CHARSET', 'UTF8');

  define('TO_DB_MYSQL_HOST', '127.0.0.1');
  define('TO_DB_MYSQL_USER', 'root');
  define('TO_DB_MYSQL_PASS', '123');
  define('TO_DB_MYSQL_DBNAME', 'p');
  define('TO_DB_MYSQL_PORT', 3306);
  define('TO_DB_MYSQL_CHARSET', 'UTF8');
 */

require_once FRAMEWORK_PATH . '/Libs/Snoopy.class.php';
require_once FRAMEWORK_PATH . '/Libs/dedehttpdown.class.php';
require_once FRAMEWORK_PATH . '/Common/string.function.php';
require_once FRAMEWORK_PATH . '/Common/global.function.php';
require_once FRAMEWORK_PATH . '/Common/h2p.function.php';
require_once FRAMEWORK_PATH . '/Common/db.function.php';
require_once FRAMEWORK_PATH . '/Common/misc.function.php';
require_once FRAMEWORK_PATH . '/Common/spider.function.php';
require_once FRAMEWORK_PATH . '/Common/trans.function.php';
require_once FRAMEWORK_PATH . '/Common/mail.function.php';
require_once FRAMEWORK_PATH . '/Common/parse.function.php';

require_once FRAMEWORK_PATH . '/Class/Log.class.php';
require_once FRAMEWORK_PATH . '/Class/Task.class.php';
require_once FRAMEWORK_PATH . '/Class/Mail/Mailer.class.php';
require_once FRAMEWORK_PATH . '/Class/Html.class.php';
require_once FRAMEWORK_PATH . '/Class/Spider.class.php';
require_once FRAMEWORK_PATH . '/Class/WinCron.class.php';

require_once FRAMEWORK_PATH . '/Tools/discuz/discuz.php';
require_once FRAMEWORK_PATH . '/Tools/QQ/qq.php';

require_once FRAMEWORK_PATH . '/Spider/Dangdang.php';
require_once FRAMEWORK_PATH . '/Spider/360buy.php';

require_once FRAMEWORK_PATH . '/Tools/SuperUpdate/discuz.php';

fCheckDir(APP_PATH);
fCheckDir(APP_LOG_PATH);
fCheckDir(APP_DATA_PATH);
fCheckDir(OUTPUT_LOGDIR);
if (defined('APP_USER_EMAIL_HAS_REGISTER_FILE')) {
    fCheckDir(APP_USER_EMAIL_HAS_REGISTER_FILE);
    fCheckDir(APP_USER_PASS_UID_REGISTER_FILE);
}
?>